import {
  setPageTitleAndBgcolor,
  checkDeviceStatus,
  getAuthorize,
  showFailModal,
} from '../../../util/util';
const Location = require('../../../util/location');
const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    systemConfig: app.globalData.systemConfig,
    show: false,
    pointContent: {}, //point content
    userInfo: {}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      systemConfig: app.globalData.systemConfig
    });
    // setPageTitleAndBgcolor(this.data.systemConfig.languageObj.menu[0].gps, this.data.systemConfig.theme == 'dark' ? '#151515' : '#FFFFFF');
    checkDeviceStatus().then(res => {
      getAuthorize('scope.userLocation').then(res => {
        console.log(res, Location);
        wx.startLocationUpdate({
          type: 'gcj02',
          success: res => {
            wx.onLocationChange(this.locationChangeFn);
          },
          fail: err => {
            showFailModal(err);
          }
        })
      }).catch(err => {
        this.changePointModelData('../../img/GPS.png', 'location', [{
          icon: 'checked',
          color: "#02DD5B",
          text: '当前定位已打开'
        }, {
          icon: 'clear',
          color: 'red',
          text: '定位服务未授权'
        }], [], {
          type: 400,
          text: '前往授权'
        });
      })
    }).catch(err => {
      this.changePointModelData('../../img/GPS.png', err.type, err.content, err.desc, err.button);
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  //改变pointModel状态
  changePointModelData(icon, type, content, desc, button = {
    type: 'confirm',
    text: '确定'
  }) {
    let pointContent = this.data.pointContent;
    pointContent.type = type;
    pointContent.icon = icon;
    pointContent.content = content;
    pointContent.desc = desc;
    pointContent.button = button;
    this.setData({
      pointContent,
      show: true
    })
  },
  onClickHide() {
    this.setData({
      show: false
    });
  },
  getDeviceStatus(val) {
    console.log(val);
    if (val.detail.type == 'location') {
      switch (val.detail.code) {
        //未授权定位
        case 401:
          wx.openAppAuthorizeSetting({
            success: res => {
              console.log(res);
            },
            fail: err => {
              showFailModal(err);
            }
          });
          break;
        case 400:
          wx.openSetting();
      }
    } else {
      // switch (val.detail.code) {
      //   //蓝牙未开启
      //   case 500:
      //     getDeviceType() == 1 ?
      //       wx.openSystemBluetoothSetting({
      //         success: res => {
      //           this.data.isGotoAndroidSysSetting = true; //设置安卓跳转系统设置页面
      //           console.log(res);
      //         },
      //         fail: err => {
      //           showFailModal(err);
      //         }
      //       }) :
      //       '';
      //     break;
      //   case 401:
      //     wx.openAppAuthorizeSetting({
      //       success: res => {
      //         console.log(res);
      //       },
      //       fail: err => {
      //         showFailModal(err);
      //       }
      //     });
      //     break;
      // }
    }
    this.onClickHide();
  },
  locationChangeFn(res) {
    console.log(res);
    this.data.userInfo = res;
  }
})